<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" import="Connect.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link rel="stylesheet" type="text/css" href="style.css" media="screen" />
<title>Hammer Time Auctions</title>
<%
	// Id of desired item.
	String item_id = request.getParameter("id");
	if (item_id == null) {
		item_id = (String) session.getAttribute("item_id");
	}
	String item_name = null;

	// Getting information and updating the database.
	java.sql.Connection conn = null;
	try {
		// Connect to the database.
		Class.forName(ConnectionInfo.myJDBCDriver()).newInstance();
		java.util.Properties sysprops = System.getProperties();
		sysprops.put("user", ConnectionInfo.myUserID());
		sysprops.put("password", ConnectionInfo.myPassword());
		conn = java.sql.DriverManager.getConnection(
				ConnectionInfo.myURL(), sysprops);

		// Get the item information.
		java.sql.Statement statement = conn.createStatement();
		java.sql.ResultSet rs = statement
				.executeQuery("SELECT * FROM items WHERE id='"
						+ item_id + "'");
		if (rs.next()) {
			item_name = rs.getString("name");
		}
%>
<script language="javascript" type="text/javascript">
	function viewClicked(button) {
		var myForm = document.createElement("form");
		myForm.setAttribute('method', "post");
		myForm.setAttribute('action', "view_bid_history.jsp");
		var data = document.createElement("input");
		data.setAttribute('type', "hidden");
		data.setAttribute('name', "auction_id");
		data.setAttribute('value', button.title);
		myForm.appendChild(data);
		var data2 = document.createElement("input");
		data2.setAttribute('type', "hidden");
		data2.setAttribute('name', "item_id");
		data2.setAttribute('value', "<%=item_id%>");
		myForm.appendChild(data2);
		var data3 = document.createElement("input");
		data3.setAttribute('type', "hidden");
		data3.setAttribute('name', "item_name");
		// @formatter:off
		data3.setAttribute('value',"<%=item_name%>");
		// @formatter:on
		myForm.appendChild(data3);
		javascript: myForm.submit();
	}
	function bidClicked(number) {
		document.forms['bids'].elements["id"].value = number;
		javascript: bids.submit();
	}
</script>
</head>
<body>
	<div id="content">
		<%@include file="header.jsp"%>
		<h1>
			View Auctions For
			<%=item_name%></h1>
		<hr></hr>
		<%
			// Create a back button.
				out.println("<a href=\"view_items.jsp\" class=\"margin-5px button\">Back</a>");
		%>
		<table class="center item-table">
			<tr>
				<th class="left-text padding-5px">Item Id: <%=" " + rs.getString("id")%></th>
				<th></th>
			</tr>
			<tr>
				<%@ page import="java.io.File"%>
				<%
					String imagePath = application.getRealPath("item_images") + "\\" + rs.getString("id") + ".png";
						File file = new File(imagePath);
						if (!file.exists()) {
							//imagePath = application.getRealPath("item_images") + "\\" + "0" + ".png";
							imagePath = "images/logo.png";
						} else {
							// Runtime correctioin. Mostly has to do with local servers.
							imagePath = imagePath.substring(imagePath.indexOf("item_images"));
						}
				%>
				<td style="vertical-align: top" width="128" height="128"><img src="<%=imagePath%>" alt="item image" width="128" height="128"></td>
				<td>
					<form name="create_auction" action="create_auction_validate.jsp" method="post">
						<table class="inner-table">
							<tr>
								<th>Name:</th>
								<td><%=" " + rs.getString("name")%></td>
							</tr>
							<tr>
								<th>Year:</th>
								<td><%=" " + rs.getString("year")%></td>
							</tr>
							<tr>
								<th>Type:</th>
								<td><%=" " + rs.getString("type")%></td>
							</tr>
							<tr>
								<th>Copies Available:</th>
								<td><%=" " + rs.getString("copies_available")%></td>
							</tr>
							<tr>
								<th>Copies Sold:</th>
								<td><%=" " + rs.getString("copies_sold")%></td>
							</tr>
							<tr>
								<th>Description:</th>
								<td><%=" " + rs.getString("description")%></td>
							</tr>
						</table>
					</form>
		</table>
		<br />
		<form name="bids" action="place_bid_validate.jsp" method="post">
			<input type="hidden" name="id" value="<%=rs.getString("id")%>" /> <input type="hidden" name="item_id" value="<%=item_id%>" />
			<%
				// Get the auction information.
					rs = statement.executeQuery("SELECT auctions.*, customers.rating FROM auctions"
							+ " RIGHT JOIN customers ON auctions.owner = customers.username WHERE item='" + item_id + "' AND end_date > NOW() ");
					if (rs != null) {
						while (rs.next()) {
			%>
			<table class="center item-table">
				<tr>
					<th class="left-text padding-5px"><a id=<%=" " + rs.getString("id")%>>Auction Id: <%=" " + rs.getString("id")%></a></th>
					<th></th>
				</tr>
				<tr>
					<td><table class="inner-table">
							<tr>
								<th>Current Bid:</th>
								<td><b> <%
 	// Get current bid.
 				java.sql.Connection conn2 = null;
 				conn2 = java.sql.DriverManager.getConnection(ConnectionInfo.myURL(), sysprops);
 				java.sql.Statement statement2 = conn2.createStatement();
 				// Get current bids.
 				java.sql.ResultSet rs2 = statement2.executeQuery("SELECT * FROM bids WHERE auction_id='" + rs.getString("id")
 						+ "' ORDER BY auction_id DESC, bid_price DESC, bid_time ASC");
 				String current_bid = "0.00";
 				if (rs2.next()) {
 					double highest_bid = Double.parseDouble(rs2.getString("bid_price"));
 					String highest_bidder = rs2.getString("bidder_username");
 					boolean next_bid = rs2.next();
 					if (next_bid == false) {
 						out.println(rs.getString("minimum_bid"));
 						current_bid = ""
 								+ ConnectionInfo.twoSigFigs(Double.parseDouble(rs.getString("minimum_bid"))
 										+ Double.parseDouble(rs.getString("bid_increment")));
 					} else {
 						double next_highest_bid = Double.parseDouble(rs2.getString("bid_price"));
 						if (next_highest_bid < highest_bid) {
 							highest_bid = next_highest_bid + Double.parseDouble(rs.getString("bid_increment"));
 						}

 						highest_bid = ConnectionInfo.twoSigFigs(highest_bid);
 						String highest_bid_string = "" + ConnectionInfo.twoSigFigs(highest_bid);
 						current_bid = "" + ConnectionInfo.twoSigFigs(highest_bid + Double.parseDouble(rs.getString("bid_increment")));
 						int length = highest_bid_string.length();
 						int index = highest_bid_string.indexOf('.');
 						while (index > length - 3) {
 							highest_bid_string += "0";
 							length = highest_bid_string.length();
 						}
 						if (index < length - 3) {
 							highest_bid_string = highest_bid_string.substring(0, index + 3);
 						}
 						out.println(highest_bid_string);

 						// Minimum bid the user needs.
 						highest_bid_string = "" + ConnectionInfo.twoSigFigs(highest_bid + Double.parseDouble(rs.getString("bid_increment")));
 						length = highest_bid_string.length();
 						index = highest_bid_string.indexOf('.');
 						while (index > length - 3) {
 							highest_bid_string += "0";
 							length = highest_bid_string.length();
 						}
 						if (index < length - 3) {
 							highest_bid_string = highest_bid_string.substring(0, index + 3);
 						}
 						current_bid = highest_bid_string;
 					}
 					//out.println(rs2.getString("bid_price"));
 					if (highest_bid >= rs.getDouble("reserve_price") && rs.getDouble("reserve_price") > 0.00) {
 						out.println(" [ Reserve Met ]");
 					} else if (rs.getDouble("reserve_price") > 0.00) {
 						out.println(" [ Reserve Not Met ]");
 					}
 					if (highest_bidder.equalsIgnoreCase((String) session.getAttribute("logged_user"))) {
 						out.println(" [ You are the highest bidder!!! ]");
 					}
 				} else {
 					out.println("NONE");
 					current_bid = rs.getString("minimum_bid");

 					if (rs.getDouble("reserve_price") > 0.00) {
 						out.println(" [ Reserve Not Met ]");
 					}
 				}
 %>
								</b></td>
							</tr>
							<tr>
								<th>Minimum Bid:</th>
								<td><%=" " + rs.getString("minimum_bid")%></td>
							</tr>
							<tr>
								<th>Bid Increment:</th>
								<td><%=" " + rs.getString("bid_increment")%></td>
							</tr>
							<tr>
								<th>Open Date:</th>
								<td><%=" " + rs.getString("open_date")%></td>
							</tr>
							<tr>
								<th>End Date:</th>
								<td><%=" " + rs.getString("end_date")%></td>
							</tr>
							<tr>
								<th>Owner:</th>
								<td>
									<%
										out.println("<a href=\"customer_auction.jsp?auc=" + rs.getString("owner") + "\">" + rs.getString("owner") + "</a>");
									%> <label>(<%=rs.getString("rating")%>)
								</label>
								</td>
							</tr>
						</table></td>
					<td class="align-top">
						<%
							String user_type = (String) session.getAttribute("user_type");
										if (user_type.equalsIgnoreCase("2")) {
											out.println("<a href=\"#\" class=\"right button\" onclick=\"return bidClicked(" + rs.getString("id")
													+ ")\">Place Bid</a>");
											out.println("<input name=\"bid" + rs.getString("id") + "\" type=\"text\" value=\"" + current_bid + "\" class=\"right\"/>");
											out.println("<br/><br/>");
										}
						%> <a href="#" class="right button" onclick="return viewClicked(this)" title="<%=rs.getString("id")%>">View Bid History</a>
					</td>
				</tr>
			</table>
			<br />
			<%
				}
					}
			%>
		</form>
	</div>
	<hr></hr>
	<%@include file="footer.jsp"%>
</body>
</html>
<%
	} catch (Exception e) {
		e.printStackTrace();
		out.print(e.toString());
	} finally {
		try {
			conn.close();
		} catch (Exception ee) {
		}
	}
%>